import { cancelAllPendingRequests } from "@/utils/axios/index"
import { type RouteRecordRaw, createRouter, createWebHistory } from "vue-router"

const routes: RouteRecordRaw[] = [
    {
        path: "/",
        redirect: "/index",
    },
    {
        path: "/index",
        name: "index",
        component: ()=> import("@/components/index.vue"),
        children: [
            {
                path: "",
                name: "index-home",
                component: ()=> import("@/components/canlendar.vue"),
                meta: {
                    title: "美食日历"
                }
            },
            {
                path: "random",
                name: "index-random",
                component: ()=> import("@/components/random.vue"),
                meta: {
                    title: "随机美食",
                }
            },
            {
                path: "score",
                name: "score",
                component: ()=> import("@/components/score.vue"),
                meta: {
                    title: "美食打分"
                }
            }
        ],
        meta: {
            title: "首页",
        }
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from) => {
    cancelAllPendingRequests();
    if(to.meta.title) document.title = to.meta.title as string;
    else document.title = "美食管理";
})
export default router